'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const {type } = event
	if(type==='login'){
		if(event.isSign ===0 ){
			// 登录
		try{
			const getlog = await db.collection('mSign').where({userName:event.userName,password:event.password}).get();
			if(getlog.data.length>0){
				
				return msg('S','登录成功',getlog.data[0])
			}else{
				return msg('N','登录失败，请稍后重试')
			}
		}catch(e){
			console.log('错误日志',e);
		}
		}else{
			// 注册
			try{
				const setlog = await db.collection('mSign').add(event)
				console.log(setlog,'setlog------');
				if(Object.keys(setlog).length>0){
					return msg('S','注册成功')
				}else{
					return msg('N','注册失败')
				}
			}catch(e){
				console.log('捕获系统错误',e);
				const err = await db.collection('mSign').where({userName:event.userName}).get()
				if(err.data.length>0){
					return msg('N','当前账号已被注册')
				}
				return msg('N','注册失败，请稍后再试')
			}
		}
	}else if(type==='add'){
		// 记账
		const list = await db.collection('jRecord').add({...event})
		if(Object.keys(list).length>0){
			return msg('S','添加成功')
		}
		return msg('N','添加失败，请稍后重试')
	}else if(type==='get'){
		// 默认查询当前月份的数据
		let filer = {id:event.id,searchDate:event.date}
		// 当前要是有分类的话，进行分类查询
		if(event.checkType){
			filer.checkType = event.checkType
		}
		if(event.isType){
			filer.isType = event.isType
		}
		let  getList ={data:[]}
		if(event.size&&event.page){
			getList =  await  db.collection('jRecord').where(filer).orderBy('date','desc').skip((event.page-1)*event.size).limit(event.size).get()
		}else{
			getList =  await  db.collection('jRecord').where(filer).orderBy('date','desc').get()
		}
		if(getList.data.length>0){
			const total = await  db.collection('jRecord').where(filer).count()
			return msg('S','查询成功',getList.data,total.total)
		}
		return msg('N',getList.data.length==0?'当前还没有数据，快去添加吧~':'查询失败，请稍后再试',[])
	}else if(type==='del'){
		const delList = await db.collection('jRecord').doc(event.id).remove()
		if(delList.deleted>0){
			return msg('S','删除成功')
		}
		return msg('N','删除失败，请稍后再试')
	}else if(type==='edit'){
		const editList = await db.collection('jRecord').doc(event.doc).update({...event})
		if(editList.updated>0){
			return msg('S','修改成功')
		}
		return msg('N','修改失败，请稍后重试')
	}else if(type==='getType'){
		const getType = await db.collection('typeList').where({id:event.id}).get()
		if(getType.data.length>0){
			return msg('S','查询成功',getType.data[0].data)
		}
		return msg('N','查询失败，请稍后重试')
	}else if(type==='editType'){
		if(event.isEdit !=null){
			// 修改
			const editType = await db.collection('typeList').where({id:event.id}).update({
				data:{
					[event.pindex]:{
						list:{
							[event.isEdit]:{
								title:event.title
							}
						}
					}
				}
			})
			if(editType.updated>0){
				return msg('S','修改成功')
			}
			return msg('N','修改失败，请稍后重试')
		}else{
			// 添加
			const addType = await db.collection('typeList').where({id:event.id}).update({
				data:{
					[event.pindex]:{
						list:dbCmd.push([event.arrObj])
					}
				}
			})
			if(addType.updated>0){
				return msg('S','添加成功')
			}
			return msg('N','添加失败，请稍后重试')
		}
	}else if(type==='delType'){
		const delType = await db.collection('typeList').where({
			id:event.id,
		}).update({
			data:{
				[event.pindex]:{
					list: dbCmd.pull({title:event.title})
				}
			}
		})
		if(delType.updated>0){
			return msg('S','删除成功')
		}
		return msg('N','删除失败，请稍后再试')
	}
	//返回数据给客户端
	return event
};
function msg(status='S',msg='',data=[],total=0){
	return{
		status,
		msg,
		data,
		total
	}
}